import {Component, OnInit} from "@angular/core";
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
import {ActivatedRoute} from "@angular/router";
import {Location} from "@angular/common";
import {MinerService} from "../miner.service";

@Component({
  selector: 'app-miner-item-edit',
  templateUrl: './miner-item-edit.component.html',
  styleUrls: ['./miner-item-edit.component.css']
})
export class MinerItemEditComponent implements OnInit {
  public isConfirmLoading: boolean = false;
  public code: string; //minerItem
  public validateForm: FormGroup;//企业登录的表单

  constructor(private fb: FormBuilder, private minerService: MinerService, private route: ActivatedRoute, public location: Location) {
    this.validateForm = this.fb.group({
      price: [null, Validators.compose([Validators.required])], // 价格
      dailyIncome: [null, Validators.compose([Validators.required])], // 日收益
      power: [null, Validators.compose([Validators.required])], // 算力
      upperLimit: [null, Validators.compose([Validators.required])], // 挖矿上限
      multiple: [null, Validators.compose([Validators.required])], // 上限倍数
      jlb: [null, Validators.compose([Validators.required])], // 金兰宝单买价格
      active: [null, Validators.compose([Validators.required])], // 金兰宝单买激活币价格
      mixJlb: [null, Validators.compose([Validators.required])], // 混合买金兰宝价格
      mixOre: [null, Validators.compose([Validators.required])], // 混合买矿石价格
      mixActive: [null, Validators.compose([Validators.required])], // 混合买激活币价格
      isActiveCoin: ['Y', Validators.compose([Validators.required])], // 是否需要激活币
      inventory: [null, Validators.compose([Validators.required])], // 库存
      separateLimit: [null, Validators.compose([Validators.required])] // 商城分割量
    });
  }

  ngOnInit() {
    this.route.url.subscribe(url => {
      const curPath = url[0].path;
      if (curPath === 'modify') {
        this.code = this.route.snapshot.params.code;//获取参数
        this.loadMinerItemInfo();
      }//修改前查询出供应商信息
    })
  }

  /**
   * 表单确认
   */
  addMinerItem($event) {
    $event.preventDefault();
    //1.进行脏检查，提示未填的必填字段
    for (const key in this.validateForm.controls) {
      this.validateForm.controls[key].markAsDirty();
      this.validateForm.controls[key].updateValueAndValidity();
    }
    if (this.validateForm.invalid) return;
    this.isConfirmLoading = true;
    let formData = Object.assign({},this.validateForm.value);
    //添加minerItem
    if (this.code) {
      formData.code = this.code;
      this.minerService.modifyMinerItem(formData).then((data: any) => {
        this.isConfirmLoading = false;
        this.validateForm.reset();
        this.location.back();
      }).catch(res => this.isConfirmLoading = false)
    } else {
      this.minerService.addMinerItem(formData).then((data: any) => {
        this.isConfirmLoading = false;
        this.location.back();
      }).catch(res => this.isConfirmLoading = false)
    }
  }

  /**
   * 查询minerItem信息
   */
  loadMinerItemInfo() {
    this.minerService.loadMinerItemByCode(this.code).then((data: any) => {
      this.validateForm.patchValue(data);
    })
  }


  getFormControl(name) {
    return this.validateForm.controls[name];
  }
}
